home *** CD-ROM | disk | FTP | other *** search
- // This program contains implementation of grfile.h
- // Written by Podvoysky E. & Kiselev J. CZ 1994.
- #pragma -ml
-
- #include <stdio.h>
- #include <mem.h>
- #include <alloc.h>
- #include <dos.h>
- #include "common.h"
- #include "graph.h"
- #include "grfile.h"
- #include "manager.h"
-
- #define GRF_BUFSIZE (1<<14) //size of internal buffer
-
- long minheap = 70000L;
-
- int get_bitsperpixel(image_type it,image_extention_type iet) {
- switch(it) {
- case MONOIMG : return 1;
- case COLOR16IMG : return 4;
- case GRAY256IMG:
- case COLOR256IMG: return 8;
- case TRUECOLORIMG:
- switch(iet) {
- case RGB15: return 15;
- case RGB16: return 16;
- default: return 24;
- }
- }
- return(1);
- }
-
-
- graph_file_abstract::graph_file_abstract() {
- file_ok = usebuffer = FALSE;
- the_file = NULL;
- }
-
- graph_file_abstract:: ~graph_file_abstract() {
- if (the_file) fclose(the_file);
- if (usebuffer) delete buffer;
- }
-
- void graph_file_abstract::init_buffer(BOOL usebuffer_) {
- long pos;
-
- usebuffer = usebuffer_;
- if (usebuffer) {
- buff_offset = 0;
- buffer = new BYTE[BUFF_SIZE];
- }
- else if ((file_ok) && (coreleft() > minheap + GRF_BUFSIZE)) {
- pos = ftell(the_file);
- fseek(the_file,0,SEEK_SET);
- setvbuf(the_file, NULL, _IOFBF, GRF_BUFSIZE);
- fseek(the_file,pos,SEEK_SET);
- }
- }
-
- long graph_file_abstract::image_size() {return(bytesperline*height);}
-
- graph_file_reader::graph_file_reader(char *fname) {
- file_ok = FALSE;
- top_to_bottom = left_to_right = TRUE;
- the_file = NULL;
- source_line = NULL;
- standart_line = NULL;
- line_n = 0;
- the_file = fopen(fname, "rb");
- file_ok = (the_file != NULL);
- source_ext_type = NO_EXTENTION;
- }
-
- // if buffer is almost cleared
- // reads new portion of data
- void graph_file_reader::fresh_buffer() {
- if (!usebuffer) return;
- if(buff_offset > (BUFF_SIZE - 2*bytesperline)) {
- memmove(buffer,&buffer[buff_offset],BUFF_SIZE-buff_offset);
- fread(&buffer[BUFF_SIZE-buff_offset],buff_offset,1,the_file);
- buff_offset = 0;
- }
- }
-
- void graph_file_reader::seek_start() {
- fseek(the_file, data_begin, SEEK_SET);
- line_n = top_to_bottom ? 0 : height-1;
- if (usebuffer) {
- fread(buffer, BUFF_SIZE, 1,the_file);
- buff_offset = 0;
- }
- }
-
- int graph_file_reader::get_next_line() {return(-1);}
-
- int graph_file_reader::get_stand_line() {
- int result;
- result = get_next_line();
- if (result >= 0) line2standart(source_line,standart_line,width);
- return(result);
- }
-
- /*************** SAVER ***************************************/
-
- graph_file_saver::graph_file_saver(char *fname, BOOL usebuffer_,
- image_type dest_type_,
- image_extention_type dest_ext_type_,
- int width_,int height_,BGRpalette* pal) {
- top_to_bottom = left_to_right = TRUE;
- file_ok = FALSE;
- the_file = NULL;
- line_n = 0;
- dest_type = dest_type_;
- dest_ext_type = dest_ext_type_;
- palette = (BGRpalette*)pal;
- width = width_;
- height = height_;
- the_file = fopen(fname, "wb");
- file_ok = (the_file != NULL);
- init_buffer(usebuffer_);
- }
-
- int graph_file_saver::put_next_line(BYTE *line) {return -1;}
-
- void graph_file_saver::flush_buffer() {
- if (!usebuffer) return;
-
- fwrite(buffer,buff_offset,1,the_file);
- buff_offset = 0;
- }
-
- int graph_file_saver::put_stand_line(BYTE *line) {
- int result;
- BYTE *tmp;
- tmp = new BYTE [bytesperline+1];
- line2standart(line,tmp,width);
- result = put_next_line(tmp);
- delete tmp;
- return(result);
- }
-
- image_keeper::image_keeper(memory_type where_,
- image_type im_type_,
- int width_,int height_,BGRcolortype * pal,
- BOOL top_to_bottom_) {
- long imagesize;
- int i;
-
- width = width_; height = height_; top_to_bottom = top_to_bottom_;
- left_to_right = TRUE;
- line_n = top_to_bottom ? 0 : height-1;
-
- bytesperline = width;
- switch(im_type_) {
- case MONOIMG : im_type = MONOIMG;
- bytesperline = (width+7) / 8;
- break;
- case COLOR16IMG :
- case GRAY256IMG:
- case COLOR256IMG: im_type = COLOR256IMG;
- break;
- case TRUECOLORIMG: im_type = TRUECOLORIMG;
- bytesperline *= 3;
- break;
- }
- if (im_type == COLOR256IMG) {
- palette = new BGRcolortype[256];
- memcpy(palette, pal, sizeof(BGRpalette));
- }
- else palette = NULL;
-
- imagesize = (long)bytesperline*(long)height;
-
- where = where_;
- if (where == UNKNOWN) {
- if (coreleft() > imagesize + minheap) where = convRAM;
- else if (EXM_initialized && (EXM_coreleft() > imagesize + 0x10000L)) where = exRAM;
- else where = DISK;
- }
-
- switch (where) {
- case convRAM:
- lines = new BYTE* [height];
- memset(lines,0,height*sizeof(BYTE *));
- for (i = 0; i < height; i++) {
- lines[i] = new BYTE[bytesperline];
- if (lines[i] == NULL) return;
- }
- file_ok = TRUE;
- break;
- case exRAM:
- lines_ext = new USER_HANDLE[height];
- bytesperline = ((bytesperline + 1) >> 1) << 1;
- // make length even for better performance with XMS
-
- memset(lines_ext,0xFF,height*sizeof(USER_HANDLE));
- for (i = 0; i < height; i++) {
- lines_ext[i] = EXM_alloc(bytesperline);
- if (lines_ext[i] == NULL_HANDLE) return;
- }
- current_line = new BYTE[bytesperline];
- file_ok = (current_line != NULL);
- break;
-
- case DISK:
- lines = NULL;
- tmp_filename = tempnam("c:","cz$");
- the_file = fopen(tmp_filename, "w+b");
-
- file_ok = (the_file != NULL);
- init_buffer(FALSE);
- current_line = new BYTE[bytesperline];
- file_ok = file_ok && (current_line != NULL);
- break;
- }
- }
-
- image_keeper:: ~image_keeper() {
- int i;
- char *tmpptr;
- switch (where) {
- case convRAM:
- for (i = height-1; i >= 0; i--) if (lines[i] != NULL) delete lines[i];
- delete lines;
- break;
- case exRAM:
- for (i = height-1; i >= 0; i--)
- if (lines_ext[i] != NULL_HANDLE) EXM_free(lines_ext[i]);
- delete lines_ext;
- break;
-
- case DISK: fclose(the_file);
- tmpptr = tmp_filename;
- asm {
- push ds
- lds dx,tmpptr
- mov ah,0x41
- int 21h
- pop ds
- }
- free(tmp_filename);
- break;
-
- default:
- if (current_line != NULL) delete current_line;
- }
- if (palette != NULL) delete palette;
- }
-
- void image_keeper::store_line(BYTE * source) {
- if ((line_n >= height) || (line_n < 0)) return;
- switch (where) {
- case convRAM:
- memcpy( lines[line_n], source, bytesperline);
- break;
-
- case exRAM:
- if (EXM_move_to_ext(lines_ext[line_n], source, 0, bytesperline))
- file_ok = FALSE;
- break;
-
- case DISK:
- fseek(the_file,0,SEEK_END);
- fwrite(source,bytesperline,1,the_file);
- }
- if (top_to_bottom) line_n++; else line_n--;
- }
-
- BYTE* image_keeper::give_line(int line_no) {
- switch (where) {
- case convRAM:
- return(lines[line_no]);
- // break;
-
- case exRAM:
- if (line_n == line_no) return(current_line);
- if (EXM_move_to_conv(current_line, lines_ext[line_no],0,bytesperline))
- file_ok = FALSE;
- line_n = line_no;
- return(current_line);
- // break;
-
- case DISK:
- if (line_n == line_no) return(current_line);
- if (top_to_bottom) fseek(the_file,(long)line_no * bytesperline, SEEK_SET);
- else fseek(the_file,(long)(height-1-line_no) * (long)bytesperline, SEEK_SET);
- fread(current_line,bytesperline,1,the_file);
- line_n = line_no;
- return(current_line);
- // break;
- }
- }
-
-
- void image_keeper::replace_line(int line_no,BYTE * source) {
- if ((line_no >= height) || (line_no < 0)) return;
- switch (where) {
- case convRAM:
- memcpy( lines[line_no], source, bytesperline);
- break;
-
- case exRAM:
- if (EXM_move_to_ext(lines_ext[line_no], source, 0, bytesperline))
- file_ok = FALSE;
- break;
-
- case DISK:
- if (top_to_bottom) fseek(the_file,(long)line_no * bytesperline, SEEK_SET);
- else fseek(the_file,(long)(height-1-line_no) * (long)bytesperline, SEEK_SET);
- fwrite(source,bytesperline,1,the_file);
- }
- }
-